<?php

abstract class AbstractRunTimeWorkflowNodeAction
{
  
  const OPTION_LABEL = 'label';
  
  /**
   * @var RunTimeWorkflowNode $node
   */
  protected $node;
  
  /**
   * @var array $options
   */
  protected $options;
  
  /**
   * @var string $identifier
   */
  protected $identifier;

  /**
   * @param string $identifier
   * @param array $options
   */
  public function __construct (RunTimeWorkflowNode $node, $identifier, array $options = array())
  {
    $this->node = $node;
    $this->identifier = $identifier;
    $this->options = $options;
    
    $this->configure();
  }

  protected function configure ()
  {
  
  }

  /**
   * @param string $identifier
   * @param array $options
   * @return DesignTimeWorkflowNodeAction
   */
  public static function createDesignTimeAction ($identifier, array $options = array())
  {
    $class = get_called_class();
    return new DesignTimeWorkflowNodeAction($class, $identifier, $options);
  }

  /**
   * @return string
   */
  public function getLabel ()
  {
    return $this->getOption(self::OPTION_LABEL, $this->identifier);
  }

  /**
   * @return string
   */
  public function getRouteName ()
  {
    return 'wf_workflow_node_execute_action';
  }

  /**
   * @return array
   */
  public function getRouteParameters ()
  {
    return $this;
  }

  /**
   * @return AbstractRunTimeWorkflowNode
   */
  public function getNode ()
  {
    return $this->node;
  }

  /**
   * @param string $key
   */
  public function hasOption ($key)
  {
    return isset($this->options[$key]);
  }

  /**
   * @param string $key
   * @param mixed $default
   */
  public function getOption ($key, $default = null)
  {
    if ($this->hasOption($key))
    {
      return $this->options[$key];
    }
    else
    {
      return $default;
    }
  }

  /**
   * @param string $key
   * @param mixed $value
   */
  public function setOption ($key, $value)
  {
    $this->options[$key] = $value;
  }

  /**
   * @param string $key
   * @param mixed $value
   */
  public function setDefaultOption ($key, $value)
  {
    if (! $this->hasOption($key))
    {
      $this->setOption($key, $value);
    }
  }

  /**
   * @return string
   */
  public function getIdentifier ()
  {
    return $this->identifier;
  }

  /**
   * @param AbstractWorkflowContainer $container
   */
  abstract public function execute (AbstractWorkflowContainer $container);
}